package com.lxl.controller;

import cn.idev.excel.ExcelReader;
import cn.idev.excel.FastExcel;
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.read.listener.PageReadListener;
import cn.idev.excel.read.listener.ReadListener;
import cn.idev.excel.read.metadata.ReadSheet;
import com.alibaba.fastjson.JSON;
import com.lxl.bo.BankInfo;
import com.lxl.bo.DemoData;
import com.lxl.bo.ExportBank;
import com.lxl.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiulong.liu
 * @Date 2024/12月/05/星期四 14:14
 * @Desc
 */
@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {

    @PostMapping("/exportBank1")
    @ResponseBody
    public void exportBank1(@RequestBody List<ExportBank> test) {
        test.forEach(bankList1 -> {

            System.out.println("bankDetail = " + bankList1);
        });
    }

    @PostMapping("/exportBank")
    @ResponseBody
    public void exportBank(@RequestBody BankInfo bankInfo) {
        bankInfo.getBankList().forEach(bankList1 -> {

            System.out.println("bankDetail = " + bankList1);
        });
    }

    public static void main(String[] args) {
        List<ExportBank> bankList = new ArrayList<>();
        ExportBank exportBank = new ExportBank();
        exportBank.setClientCode("0000");
        exportBank.setClientName("测试银行");
        bankList.add(exportBank);
        //JSON parse = JSONUtil.parse(bankList);
        //System.out.println(parse);
    }

    @Test
    public void simpleRead() {
        String fileName = "F:\\demo.xlsx";

        // 使用方式1：Lambda表达式直接处理数据
        FastExcel.read(fileName, DemoData.class, new PageReadListener<>(dataList -> {
            for (Object demoData : dataList) {
                log.info("读取到一条数据: {}", JSON.toJSONString(demoData));
            }
        })).sheet().doRead();

        // 使用方式2：匿名内部类
        FastExcel.read(fileName, DemoData.class, new ReadListener<DemoData>() {
            @Override
            public void invoke(DemoData data, AnalysisContext context) {
                log.info("读取到一条数据: {}", JSON.toJSONString(data));
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) { }
        }).sheet().doRead();

        // 使用方式3：自定义监听器
        FastExcel.read(fileName, DemoData.class, new ExcelUtils.DemoDataListener()).sheet().doRead();

        // 使用方式4：多 Sheet 读取
        try (ExcelReader excelReader = FastExcel.read(fileName, DemoData.class, new ExcelUtils.DemoDataListener()).build()) {
            ReadSheet readSheet = FastExcel.readSheet(0).build();
            excelReader.read(readSheet);
        }
    }

}
